package towerDefense.screens;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import towerDefense.TowerDefense;
import util.BufferedImageModifier;
import com.golden.gamedev.GameEngine;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;


public class ElementSelectScreen extends MenuScreen
{

    public static final String ELEMENT_IMAGE_DIR = "src/images/menu/elements/";
    Sprite earth, earthtext, earthtextselect, fire, firetext, firetextselect,
            water, watertext, watertextselect, wind, windtext, windtextselect;
    SpriteGroup MENU_GROUP;


    public ElementSelectScreen (GameEngine arg0)
    {
        super(arg0);
    }


    @Override
    public void initResources ()
    {
        MENU_GROUP = new SpriteGroup("Element Menu");
        try
        {
            earth = new Sprite();
            createEleMenuPanel(earth, "earth.png", 0, 0);
            earthtext = new Sprite();
            initializeMenuItem(earthtext, earth, "earthtext.png", 5);
            earthtextselect = new Sprite();
            initializeMenuItem(earthtextselect, earth, "earthtextselect.png", 5);

            fire = new Sprite();
            createEleMenuPanel(fire,
                               "fire.jpg",
                               this.getWidth() / 2,
                               this.getHeight() / 2);
            firetext = new Sprite();
            initializeMenuItem(firetext, fire, "firetext.png", 5);
            firetextselect = new Sprite();
            initializeMenuItem(firetextselect, fire, "firetextselect.png", 5);

            water = new Sprite();
            createEleMenuPanel(water, "water.jpg", this.getWidth() / 2, 0);
            watertext = new Sprite();
            initializeMenuItem(watertext, water, "watertext.png", 5);
            watertextselect = new Sprite();
            initializeMenuItem(watertextselect, water, "watertextselect.png", 5);

            wind = new Sprite();
            createEleMenuPanel(wind, "wind.jpg", 0, this.getHeight() / 2);
            windtext = new Sprite();
            initializeMenuItem(windtext, wind, "windtext.png", 5);
            windtextselect = new Sprite();
            initializeMenuItem(windtextselect, wind, "windtextselect.png", 5);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }


    /**
     * @param j
     * @param i
     * @param string
     * @param ele
     * @throws IOException
     */
    private void createEleMenuPanel (Sprite ele, String string, int i, int j)
        throws IOException
    {
        ele.setImage(BufferedImageModifier.getScaledInstance(ImageIO.read(new File(ELEMENT_IMAGE_DIR +
                                                                                   string)),
                                                             this.getWidth() / 2,
                                                             this.getHeight() / 2));
        ele.setLocation(i, j);
        MENU_GROUP.add(ele);
    }


    private void initializeMenuItem (Sprite option,
                                     Sprite parent,
                                     String filename,
                                     int scalar) throws IOException
    {
        option.setImage(createMenuItem(ELEMENT_IMAGE_DIR + filename, scalar));
        option.setLocation((int) parent.getScreenX() + parent.getWidth() / 2 -
                           option.getWidth() / 2, (int) parent.getScreenY() +
                                                  parent.getHeight() / 2 -
                                                  option.getHeight() / 2);
        option.setActive(false);
        MENU_GROUP.add(option);
    }


    @Override
    public void render (Graphics2D g)
    {
        MENU_GROUP.render(g);

    }


    @Override
    public void update (long arg0)
    {
        if (this.checkPosMouse(earth, false))
        {
            earthtextselect.setActive(true);
            earthtext.setActive(false);
            if (this.click())
            {
                TowerDefense.ELEMENT_TYPE = TowerDefense.EARTH_ID;
                parent.nextGameID = TowerDefense.PLAY_SCREEN;
                finish();
            }
        }
        else
        {
            earthtextselect.setActive(false);
            earthtext.setActive(true);
        }

        if (this.checkPosMouse(fire, false))
        {
            firetextselect.setActive(true);
            firetext.setActive(false);
            if (this.click())
            {
                TowerDefense.ELEMENT_TYPE = TowerDefense.FIRE_ID;
                parent.nextGameID = TowerDefense.PLAY_SCREEN;
                finish();
            }
        }
        else
        {
            firetextselect.setActive(false);
            firetext.setActive(true);
        }

        if (this.checkPosMouse(water, false))
        {
            watertextselect.setActive(true);
            watertext.setActive(false);
            if (this.click())
            {
                TowerDefense.ELEMENT_TYPE = TowerDefense.WATER_ID;
                parent.nextGameID = TowerDefense.PLAY_SCREEN;
                finish();
            }
        }
        else
        {
            watertextselect.setActive(false);
            watertext.setActive(true);
        }

        if (this.checkPosMouse(wind, false))
        {
            windtextselect.setActive(true);
            windtext.setActive(false);
            if (this.click())
            {
                TowerDefense.ELEMENT_TYPE = TowerDefense.WIND_ID;
                parent.nextGameID = TowerDefense.PLAY_SCREEN;
                finish();
            }
        }
        else
        {
            windtextselect.setActive(false);
            windtext.setActive(true);
        }

        if (bsInput.getKeyPressed() == KeyEvent.VK_ESCAPE) finish();
    }

}
